(function () {
    "use strict";

    angular.module('core').controller('reportOperacaoVR000002Controller', controller);
    controller.$inject = ['$scope', '$timeout', 'toolsService', 'vendaVarejoService'];

    function controller($scope, $timeout, toolsService, vendaVarejoService) {
        $scope.initializeLayout('Análise de Itens Vendas de Varejo');

        //[Inicializa objetos]
        $scope.state = {
            data: [],
            carregando: false,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                args: null,
            }
        };

        //[Inicializa funcões]
        $scope.initialize = function () {
            $scope.clearFilter();
            $scope.refresh();
        };

        $scope.getAll = function () {
            $scope.state.filtro.args.skip = 0;
            $scope.state.carregando = true;
            $scope.state.vazio = false;
            vendaVarejoService
                .getAllItem($scope.state.filtro.args)
                .then(
                function (response) {
                    if (response.Status < 0) {
                        $scope.tools.toastr.error(response.Message, 'Serviço');
                        console.error(response.Exception);
                    } else {
                        angular.copy(angular.fromJson(response.Data), $scope.state.data);

                        /* Início Pivot Table */
                        var dateFormat = $.pivotUtilities.derivers.dateFormat;
                        var sortAs = $.pivotUtilities.sortAs;
                        var tpl = $.pivotUtilities.aggregatorTemplates;
                        var fmt = $.pivotUtilities.numberFormat({ suffix: " °C" });

                        $("#pivot-table").pivotUI(toolsService.flattenArray(response.Data), {
                            hiddenAttributes: [],
                            derivedAttributes: {
                                "VendaVarejo.Emissao.Ano": dateFormat("VendaVarejo.Emissao", "%y", true),
                                "VendaVarejo.Emissao.Mes": dateFormat("VendaVarejo.Emissao", "%n", true),
                                "VendaVarejo.Emissao.Dia": dateFormat("VendaVarejo.Emissao", "%d", true),
                                "VendaVarejo.Emissao.DiaSemana": dateFormat("VendaVarejo.Emissao", "%w", true),
                            },
                            sorters: {
                                "VendaVarejo.Emissao.Mes": sortAs(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]),
                                "VendaVarejo.Emissao.DiaSemana": sortAs(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]),
                            },
                            rows: ["VendaVarejo.Filial.Empresa.Nome", "SKU.Produto.Nome", "SKU.Variante.Nome", "SKU.Posicao.Nome"],
                            cols: ["VendaVarejo.Emissao.Ano", "VendaVarejo.Emissao.Mes"],
                            vals: ["ValorTotal"],
                            aggregatorName: "Sum",
                            rendererName: "Heatmap",
                            renderers: $.extend(
                                $.pivotUtilities.renderers,
                                $.pivotUtilities.c3_renderers,
                                $.pivotUtilities.d3_renderers
                            ),
                        });

                        $scope.tools.closeColunasDinamicas();

                        /* Fim Pivot Table */

                    }
                    $scope.state.carregando = false;
                    $scope.state.vazio = ($scope.state.data == 0);
                },
                function (_error) {
                    $scope.state.carregando = false;
                    $scope.tools.toastr.error('Não foi possível conectar com o(s) servidor(es)');
                    console.error(_error);
                }
                );
        };
        $scope.refresh = function () {
            $scope.getAll();
        };

        $scope.clearFilter = function () {
            $scope.state.filtro.args = {
                chaves: '',
                filtro: {
                    VendaVarejo: {
                        Status: 1
                    },
                    IndicaCancelado: false,
                    IndicaTroca: false,
                },
                filtroDe:
                {
                    VendaVarejo: {
                        Emissao: $scope.tools.getHoje()
                    }
                },
                filtroAte: null,
                sortExpression: '',
                skip: 0,
                take: 999999
            };
        };
        $scope.toggleFilter = function () {
            $scope.state.filtro.ativo = !$scope.state.filtro.ativo;
        };

        /*Inicializa funções*/
        $scope.initialize();

    }

})();